package com.healthlogger;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MainService extends Service {
	
	//Data interface exposed to the rest of the app (primarely the Activity) STARTS
	public void startDataCollection(int userID){
		this.userID = userID; 
	}
	private int userID;
	
	public void stopDataCollection(){
		
	}
	
	//use Global Configuration
	String mailAddress;
	public void setMailAddress(String mailAddress) {
		this.mailAddress = mailAddress;
	}

	public String getMailAddress(){
		return mailAddress;
	}
	//Data interface exposed to the rest of the app (primarely the Activity) ENDS
	
	
	
	
	@Override
	public void onCreate() {
		Log.w("USER", "MainService.onCreate");
		//super.onCreate();
        // Display a notification about us starting.  We put an icon in the status bar.
        showNotification();
		// TODO: Actions to perform when service is created.
	}
	@Override
	public void onDestroy()	{
		Log.w("USER", "MainService.onDestroy");
	}
	
	//main worker function in service 
	private void startBackgroundTask()
	{
		Log.w("USER", "MainService.startBackgroundTask");
	}
	
	public int getData() {return 1;}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		//startBackgroundTask(intent, startId);
		startBackgroundTask();
		return Service.START_STICKY;
	}
	
	//bind this service together with an activity
	//har vi bruge for en serviceConnection i vores Activity??
	@Override
	public IBinder onBind(Intent intent) {
		// TODO: Replace with service binding implementation.
		Log.w("USER", "MainService.onBind");
		return binder;
	}

	public class MyBinder extends Binder {
		MainService getService() {
			return MainService.this;
		}
	}
	private final IBinder binder = new MyBinder();
	
	
	/**
	 * Listing 9-9: Moving a Service to the foreground
	*/
	private void showNotification() {
		Log.w("USER", "MainService.showNotification");
		int NOTIFICATION_ID = 1;
		// Create an Intent that will open the main Activity
		// if the notification is clicked.
		Intent intent = new Intent(this, MainActivity.class);
//		PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
		PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		// Set the Notification UI parameters
		Notification notification = new Notification(R.drawable.healthlogger_icon, "Health Logger", System.currentTimeMillis());
		notification.setLatestEventInfo(this, "Health Logger", "View options...", pi);
		// Set the Notification as ongoing
		notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
		// Move the Service to the Foreground
		startForeground(NOTIFICATION_ID, notification);
	}	
}